package eva.savant.core.navigation

import eva.savant.core.view.util.Constants
import eva.savant.core.view.util.FacesUtils
import eva.savant.core.service.NameService

import javax.faces.event.ActionEvent
import javax.faces.context.FacesContext
import org.primefaces.context.RequestContext
import javax.faces.bean.SessionScoped
import javax.annotation.PostConstruct
import javax.faces.bean.ManagedBean

@ManagedBean
@SessionScoped
public class Controller implements Serializable {

  boolean windows

  boolean menu

  boolean dialog

  String  parameter

  def clickTree= false

  def currentNodeId= '0'

  String  transactionId

  Integer currentWinId

  def lsNode= new ArrayList<Node>()

  @PostConstruct
  public void init() {

    menu= true

    windows= false
  }

  public void showNavigation(){

    menu= !menu
  }

  public void showWindows(){

    windows= !windows

    RequestContext.currentInstance.update('transaction')
  }

  private boolean containsLsNode(String nodeId) {

    for(node in lsNode)

    if(node.id.equals(nodeId)) return true

    return false
  }

  private void removeLsNode(String nodeId) {

    for(int i=0; i < lsNode.size(); i++)

    if(lsNode[i].id.equals(nodeId)) {

    lsNode.remove(i);   return }
  }

  public void dialog(ActionEvent event) { try {

    dialog= !dialog

  } catch (Exception e) { FacesUtils.addErrorMessage e } }


  public void openTransaccion(ActionEvent event) { try {

    if(transactionId == null) return

    def navigationBuilder = (Builder) FacesUtils.getManagedBean(NameService.NAVIGATION_BUILDER)

    def node= navigationBuilder.getContent(transactionId)

    if(node != null) openNode(node.id)

  } catch (Exception e) { FacesUtils.addErrorMessage e } }


  public void closeTransaccion() { try {

    def navigationBuilder = (Builder) FacesUtils.getManagedBean(NameService.NAVIGATION_BUILDER)

    def nodeId= FacesUtils.getRequestParameter('nodeId') + ''

    def node = navigationBuilder.getNode(nodeId)


    RequestContext.currentInstance.update('transaction')

    RequestContext.currentInstance.update('head')


    if(node != null && node.content != null && node.content.bean != null) { removeLsNode(node.id)

    FacesContext.currentInstance.externalContext.sessionMap.remove(node.content.bean)


    if(lsNode.size() > 0) {

    navigationBuilder.navigation?.currentNode= lsNode[0]

    transactionId= navigationBuilder.navigation.currentNode.content.id

    } else navigationBuilder.navigation.currentNode.id= transactionId= null

    }

  } catch (Exception e) { FacesUtils.addErrorMessage e } }


  public void openNode(String nodeId) {

    def navigationBuilder = (Builder) FacesUtils.getManagedBean(NameService.NAVIGATION_BUILDER)

    def node = navigationBuilder.getNode(nodeId)


    if (node != null && node.isLink()) {

      if( ! new File(FacesUtils.getRealPath(node.content.pageRef)).exists() ) {

      FacesUtils.addErrorMessage 'No existe la transaccion en la ruta indicada'; return }


      if( lsNode.size() + 1 > Constants.MAX_OPEN_TRANSACTION && !containsLsNode(nodeId) ) { dialog= true; return }


      setTreeNodeSelectedState(navigationBuilder.navigation.currentNode.id, false)

      navigationBuilder.navigation.currentNode = node

      setTreeNodeSelectedState(navigationBuilder.navigation.currentNode.id, true)


      if(!containsLsNode(node.id)) lsNode.add(node)

      def id='0'; for(int i=0; i<lsNode.size(); i++) if(lsNode[i].id.equals(node.id)) id = i


      if(clickTree) { RequestContext.currentInstance.execute('showNavigation()'); clickTree= false }

      RequestContext.currentInstance.update('head')


      if(windows) {

      RequestContext.currentInstance.execute('W'+id+'.show()')

      RequestContext.currentInstance.update('F'+id+':'+'W'+id) }

      else { RequestContext.currentInstance.update('transaction') }


      transactionId= node.content.code

      currentNodeId= id


      FacesUtils.getHttpSession(false).setAttribute('open-usr-TR',transactionId+'')

    } else if (node != null && !node.isLink()) toggleTreeNodeExpandedState(node.id)

  }

  public void navigationEvent() {

    if(FacesUtils.getRequestParameter('tree') != null ) clickTree= true

    openNode(FacesUtils.getRequestParameter('nodeId') + '')
  }

  private void setTreeNodeSelectedState(String nodeId, boolean value) {

    def node = findTreeNode(nodeId)

    if (node == null) return

    node.selected= value

    if(value) {

    for(int i=0; i<lsNode.size(); i++)

    if(lsNode[i].id.equals(nodeId)) { currentWinId= i; break } }
  }

  private void toggleTreeNodeExpandedState(String nodeId) {

    def node = findTreeNode(nodeId)

    if (node == null) return

    node.expanded= !node.isExpanded()
  }

  public Content getCurrentContextDescriptor() {

    def navigationBuilder = (Builder) FacesUtils.getManagedBean(NameService.NAVIGATION_BUILDER)

    return navigationBuilder.navigation.currentNode.content
  }

  private Node findTreeNode(String nodeId) {

    def navigationBuilder = (Builder) FacesUtils.getManagedBean(NameService.NAVIGATION_BUILDER)
    
    return navigationBuilder.getNode(nodeId)
  }

}
